package org.egedede.commons.ui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JList;
import javax.swing.JPopupMenu;

public class JListHandlerListener<T> extends MouseAdapter {

	private final JPopupMenu popup = new JPopupMenu();

	private final JList<T> list;

	public JListHandlerListener(final JList<T> list) {
		this.list = list;
	}

	@Override
	public void mousePressed(final MouseEvent e) {
		this.maybeShowPopup(e);
	}

	@Override
	public void mouseReleased(final MouseEvent e) {
		this.maybeShowPopup(e);
	}

	private void maybeShowPopup(final MouseEvent e) {
		if (e.isPopupTrigger()) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	public void addAction(final EAbstractAction action) {
		popup.add(action);
	}

}
